<?php
namespace  App\Services;

use App\Tools\ExcelTool;
use Illuminate\Support\Facades\DB;

class UserAddressExportService {

    public function ReadExcelInfo(){
        $file = base_path().'/storage/app/temp/2019年度中国公路建设行业协会科学技术进步奖评选结果.xlsx';
        $data = (new ExcelTool())->Import($file);
        $data = array_slice($data,2);
        $data = array_format($data,'B');
        $projectList = array_column($data,'B');
        $result = DB::table('applications as a')
            ->leftJoin('application_form_data as u','u.id','=','a.id')
            ->whereIn('project_name',$projectList)
            ->select('a.id','a.project_name','u.form_data')
            ->get()
            ->toArray();

//        $resProjectList = array_column($result,'project_name');
//        dump(array_diff($projectList,$resProjectList));
//        dump($projectList);die;
        $exportData = [];
        //$counter = 0;
        foreach ($result as $val) {
            try {
                //$counter++;
                $form_data = json_decode($val->form_data,true);
                $main_company = $form_data['basic_situation']['declare_company'];

                $info = null;
                foreach ($form_data['finish_company'] as $v) {
                    if ($main_company == $v['company_name']) {
                        $info = $v;
                        break;
                    }
                }

                if (empty($info)) {
                    dump($val->project_name);
                    $info = $form_data['finish_company'][0];
                    //continue;
                }

                $item = $data[$val->project_name];
                $item['F'] = $info['company_address'];
                $item['G'] = $info['company_tel'];
                $item['H'] = $info['link_man'];
                $item['I'] = $info['phone'];
                $exportData[] = $item;
            } catch (\Exception $e) {
                continue;
            }
        }

        array_multisort($exportData,SORT_ASC,array_column($exportData,'A'));
        //dump($exportData);die;

        $header = [
            '序号',
            '获奖项目',
            '奖励等级',
            '获奖单位',
            '研发人员',
            '单位通讯地址',
            '单位电话',
            '联系人',
            '移动电弧'
        ];
        $res = (new ExcelTool())->Export($exportData,$header,base_path().'/storage/app/temp/2019公路协会获奖名单.xlsx');
        dump($res);die;
    }

}
